<?php

class File
{
    public $error;

    public $fullname;
    public $name;
    public $basename;
    public $extension;
    public $dirname;

    public $size;
    public $datetime;
    public $info = null;
    public $infolevel = fcFast;

    public function __construct($filename, $infomode)
    {

        $this->fullname = $filename;
        
        if(! is_file($filename))
            return;

        switch ($infomode)
        {
            case fcBaseInfo:
                {
                    $this->GetBaseInfo();
                    break;
                }
            case fcFullInfo:
                {
                    $this->GetFullInfo();
                    break;
                }
        }

    }

    public function GetBaseInfo($anyway = false)
    {
        if($this->infolevel < fcBaseInfo || $anyway)
        {
            $pathinfo = pathinfo($this->fullname);

            $this->basename  = $pathinfo['basename']; //with extension
            $this->name      = $pathinfo['filename']; //withOUT extension
            $this->extension = $pathinfo['extension'];
            $this->dirname   = $pathinfo['dirname'];
            $this->size      = filesize($this->fullname);
            $this->datetime  = filectime($this->fullname);

            $this->infolevel = fcBaseInfo;
        }
    }

    public function GetFullInfo()
    {
        $this->GetBaseInfo();

        if($fileInfo = FIPManager::GetInfoPlugin($this->extension))
        {
            $this->info = $fileInfo->GetInfo($this->fullname);
            $this->infolevel = fcFullInfo;
            return true;
        } else
            return false;
    }

    public function MoveTo($dest)
    {
        if(rename($this->fullname, $dest))
        {
            $this->fullname = $dest;
            if($this->infolevel >= fcBaseInfo)
                $this->GetBaseInfo(true);

            return true;
        } else
            return false;
    }

    //нужно ли?
    public function Rename($newName)  //быстрое переименование файла В ЭТОМ ЖЕ каталоге

    {
        $this->GetBaseInfo();
        $pathinfo = pathinfo($newName);

        if(rename($this->fullname, $this->dirname.DIR_SEPARATOR.$pathinfo['basename']))
        {
            $this->fullname = $this->dirname.DIR_SEPARATOR.$pathinfo['basename'];
            if($this->infolevel >= fcBaseInfo)
                $this->GetBaseInfo(true);

            return true;
        } else
            return false;
    }

    public function CopyTo($dest)
    {
        return copy($this->fullname, $dest);
    }

    public function Delete()
    {
        return unlink($this->fullname);
    }

};

class Files
{
    static function GetFile($filename, $infomode = fcBaseInfo)
    {
        if(! file_exists($filename))
            return null;
        else
            return new File($filename,$infomode);
    }

    //Get regex pattern
    static function GetRawFileList($dir, $pattern, $filetype = ftFiles)
    {
        if ($handle = opendir($dir))
        {
            $res = array();
            while (false !== ($file = readdir($handle)))
            {
                $filepath = $dir.DIR_SEPARATOR.$file;
                if(( ($filetype == ftDirs && is_dir($filepath)) || ($filetype == ftFiles && is_file($filepath) ) || $filetype == ftAll) && preg_match($pattern, $file))
                {
                    $res[] = $filepath;
                }
            }
            closedir($handle);
            return $res;
        } else
            return false;
    }

    static function GetFileList($dir, $pattern, $infomode = fcBaseInfo, $filetype = ftFiles, $sort = false, $sortdesc = false)
    {
        $res = array();
        foreach(Files::GetRawFileList($dir, $pattern, $filetype) as $filename)
        {
            $res[$filename] = Files::GetFile($filename, $infomode);
        }

        if($sort)
        {
           if($sortdesc)
               krsort($res);
           else
               ksort($res);
        }


        return $res;
    }

    static function CreateDir($dir, $mode = 0777)
    {
        mkdir($dir, $mode, true);
        /*$dirs = explode(DIR_SEPARATOR, $dir);
        $cdir = "";
        foreach($dirs as $d)
        {
            $cdir .= $d.DIR_SEPARATOR;
            if(!is_dir($cdir))
            {
                mkdir($cdir, $mode);
            }
        }*/

    }

};

?>
